package com.tourism.user.cn.controller;
/*********************************************************************
 Copyright(c) 2024 All Rights Reserved .
 Author : liliang
 CreatedOn : 2024/12/30 23:57
 Email : 2061996582@qq.com
 Describe : 用户相关的Controller
 UseCase :
 Version : 2024|V1.0.0.0
 **********************************************************************/

import com.tourism.core.cn.dto.LoginDto;
import com.tourism.core.cn.dto.RegisterDto;
import com.tourism.core.cn.dto.RegisterResultDto;
import com.tourism.core.cn.entity.User;
import com.tourism.core.cn.enums.ResultCodeEnum;
import com.tourism.core.cn.exception.BusinessException;
import com.tourism.core.cn.result.Result;
import com.tourism.user.cn.service.IUserService;
import com.tourism.user.cn.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author new
 * @date 2024/12/30 23:57
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SmsService smsService;

    @Autowired
    private IUserService userService;

    /**
     * @author new
     * @Description: 用户注册
     * @date 2025/2/3 14:41
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    public Result register(@Validated @RequestBody RegisterDto registerDto){
        log.info("用户注册请求参数：{}", registerDto);

        //首先判断验证码是否验证正确
        boolean isValidated = smsService.verifyCode(registerDto.getMobile(), registerDto.getCode());
        if (!isValidated) {
            return Result.error(ResultCodeEnum.SMS_CODE_ERROR);
        }

        //TODO 判断是否有账号，有账号直接登录
        RegisterResultDto existUser =  userService.checkUserByMobile(registerDto.getMobile());
        if (existUser != null) {
            return Result.success(existUser);  // 如果用户存在，直接返回用户信息
        }

        //调用用户注册接口
        RegisterResultDto registerResultDto =  userService.register(registerDto);
        return Result.success(registerResultDto);
    }

    /**
     * @author new
     * @Description: 获取用户信息
     * @date 2025/2/3 14:41
     * @param userId
     * @return
     */
    @PostMapping("/info")
    public Result getUserInfo(@RequestParam Long userId) {
        User user = userService.getById(userId);
        // 处理敏感信息
        user.setPassword(null);
        return Result.success(user);
    }

    /**
     * @author new
     * @Description:用户登录
     * @date 2025/2/3 18:57
     * @param loginDto
     * @return com.tourism.core.cn.result.Result
     */
    @PostMapping("/loginByPassword")
    public Result loginByPassword(@Validated @RequestBody LoginDto loginDto) {
        //TODO 调用用户登录接口
        try {
            RegisterResultDto result = userService.loginByPassword(loginDto);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(ResultCodeEnum.LOGIN_ERROR, e.getMessage());
        }
    }
}
